// 自定义异常，返回的信息
import { HttpException, HttpStatus } from '@nestjs/common';
import { BadRequestException } from '@nestjs/common';
import { dtoErrorType } from '../services/validation.service';
import { userErrorMessages } from '../services/validation.service';

// 自定义的异常，触发异常 api 直接返回异常信息。
export class ErrorResponseException extends HttpException {
  constructor(
    public errno: number,
    public message: string,
    public data?: any,
  ) {
    super(
      {
        errno,
        message,
        data,
      },
      HttpStatus.OK,
    );
  }
}

// dto 验证失败的,以及使用到统一的错误信息的
export class ValidationException extends BadRequestException {
  constructor(
    public dtoType: dtoErrorType,
    validationErrors?: any,
  ) {
    super({
      errno: userErrorMessages[dtoType].errno,
      message: userErrorMessages[dtoType].message,
      data: validationErrors,
    });
  }
}
